package com.m3839.sdk.ym.ui

import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.m3839.sdk.ym.base.YMBaseDialogFragment
import com.m3839.sdk.ym.constant.YMCodeConstant
import com.m3839.sdk.ym.constant.YMParamConstant
import com.m3839.sdk.ym.constant.YMStringConstant
import com.m3839.sdk.ym.core.YMManager
import com.m3839.sdk.ym.core.YMStorage
import com.m3839.sdk.ym.core.YMUIStackManager
import com.m3839.sdk.ym.ext.dip2px
import com.m3839.sdk.ym.ext.getId
import com.m3839.sdk.ym.util.ConditionUtils

/**
 * @description: YMModeDialogFragment
 *
 * @author ChenBaoyang
 * @date 6/16/21 11:19
 */
open class YMModeDialogFragment : YMBaseDialogFragment() {

    private lateinit var closeBtn: ImageView
    private lateinit var titleTv: TextView
    private lateinit var contentTv: TextView
    private lateinit var openFcmTv: TextView
    private lateinit var closeFcmTv: TextView

    /**
     * 表示是否开启了防沉迷
     */
    private var isOpenFcm = false

    /**
     * 表示当前是处于夜间模式
     */
    private var isNightMode = false

    /**
     * 表示通过密码新设置的防沉迷
     */
    private var isNewFcm = false

    /**
     * 表示要关闭掉之前防沉迷的所有界面
     */
    private var isCloseAllPre = false

    private var defaultOrientation = 0

    companion object{
        fun show(activity: Activity,
                 isOpenFcm: Boolean = false,
                 isNightMode: Boolean = false,
                 isNewFcm: Boolean = false,
                 isCloseAllPre: Boolean = false,
                 tag: String) {

            val fragment = YMModeDialogFragment()
            val data = Bundle().also {
                it.putBoolean(YMParamConstant.YM_KEY_FCM_IS_OPEN, isOpenFcm)
                it.putBoolean(YMParamConstant.YM_KEY_IS_NIGHT_MODE, isNightMode)
                it.putBoolean(YMParamConstant.YM_KEY_FCM_NEW, isNewFcm)
                it.putBoolean(YMParamConstant.YM_KEY_FCM_CLOSE_ALL_PRE, isCloseAllPre)
            }
            fragment.arguments = data
            fragment.show(activity.fragmentManager, tag)
        }
    }

    override fun parseArgs() {
        defaultOrientation = YMStorage.instance.getDefaultOrientation(activity, -1)
        isOpenFcm = arguments.getBoolean(YMParamConstant.YM_KEY_FCM_IS_OPEN, false)
        isNightMode = arguments.getBoolean(YMParamConstant.YM_KEY_IS_NIGHT_MODE, false)
        isNewFcm = arguments.getBoolean(YMParamConstant.YM_KEY_FCM_NEW, false)
        isCloseAllPre = arguments.getBoolean(YMParamConstant.YM_KEY_FCM_CLOSE_ALL_PRE, false)
        if(isCloseAllPre) {
            YMUIStackManager.instance!!.removeAboveFragments(this.tag)
        }
    }

    override fun layoutName() =
        if (defaultOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) "ym_page_fcm_mode_land" else "ym_page_fcm_mode"

    override fun initView(view: View?) {
        closeBtn = view!!.findViewById(getId("btn_close"))
        titleTv = view.findViewById(getId("tv_title"))
        contentTv = view.findViewById(getId("tv_content"))
        openFcmTv = view.findViewById(getId("tv_open_fcm"))
        closeFcmTv = view.findViewById(getId("tv_close_fcm"))
    }

    override fun setContent() {
        if (isOpenFcm && !isNightMode) {
            titleTv.text = YMStringConstant.YM_FCM_MODE_OPEN_TITLE
            val content: String
            if (YMManager.instance.diffFcm()) {
                content = YMStringConstant.getModeOpenContent(
                    YMManager.instance.getHolidayPlayTime(),
                    YMManager.instance.getNormalPlayTime(),
                    YMManager.instance.getNightStartTime(),
                    YMManager.instance.getNightEndTime()
                )
            } else {
                content = YMStringConstant.getModeOpenContentEx(
                    YMManager.instance.getNormalPlayTime(),
                    YMManager.instance.getNightStartTime(),
                    YMManager.instance.getNightEndTime()
                )
            }
            contentTv.text = content
            openFcmTv.text = YMStringConstant.YM_FCM_MODE_MODIFY_PASSWORD_BUTTON
            closeFcmTv.text = YMStringConstant.YM_FCM_MODE_CLOSE_BUTTON
            closeFcmTv.visibility = View.VISIBLE
        } else if (isOpenFcm) {
            titleTv.text = YMStringConstant.YM_FCM_NIGHT_MODE_TITLE
            val content: String = YMStringConstant.getNightModeContent(
                YMManager.instance.getNightStartTime(),
                YMManager.instance.getNightEndTime()
            )
            contentTv.text = content
            openFcmTv.text = YMStringConstant.YM_FCM_EXIT_GAME
            closeFcmTv.text = YMStringConstant.YM_FCM_MODE_CLOSE_BUTTON
            closeFcmTv.visibility = View.VISIBLE
        } else {
            titleTv.text = YMStringConstant.YM_FCM_MODE_TITLE
            val content: String = YMStringConstant.getModeContent(
                YMManager.instance.getTodayPlayTime(),
                YMManager.instance.getNightStartTime(),
                YMManager.instance.getNightEndTime()
            )
            contentTv.text = content
            if (defaultOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                val layoutParams: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
                    activity.dip2px(224f),
                    activity.dip2px(40f)
                )
                layoutParams.gravity = Gravity.CENTER
                layoutParams.weight = 0f
                openFcmTv.layoutParams = layoutParams
            }
            openFcmTv.text = YMStringConstant.YM_FCM_MODE_OPEN_BUTTON
            closeFcmTv.visibility = View.GONE
        }
    }

    override fun initListener() {
        if (isNightMode) {
            closeBtn.visibility = View.GONE
        } else {
            closeBtn.visibility = View.VISIBLE
        }
        closeBtn.setOnClickListener { finishEx() }

        openFcmTv.setOnClickListener(View.OnClickListener {
            if (ConditionUtils.isFastDoubleClick) {
                return@OnClickListener
            }
            if (isOpenFcm && isNightMode) {
                YMManager.instance.handleCallBack(
                    YMCodeConstant.YM_CODE_EXIT_GAME,
                    YMCodeConstant.YM_MSG_EXIT_GAME
                )
                YMUIStackManager.instance!!.finishAllFragment()
            } else if (isOpenFcm) {
                YMPasswordDialogFragment.show(activity,
                    YMParamConstant.YM_VALUE_ACTION_MODIFY_PASSWORD,
                    YMStringConstant.YM_FCM_MODIFY_PASSWORD,
                    YMStringConstant.YM_FCM_MODIFY_PASSWORD_TIP,
                    YMStringConstant.YM_FCM_NEXT_STEP,
                    "",
                    YMStringConstant.YM_FRAGMENT_TAG_PWD_MODIFY
                )
            } else {
                YMPasswordDialogFragment.show(activity,
                    YMParamConstant.YM_VALUE_ACTION_SET_PASSWORD,
                    YMStringConstant.YM_FCM_SET_PASSWORD,
                    YMStringConstant.YM_FCM_SET_PASSWORD_TIP,
                    YMStringConstant.YM_FCM_NEXT_STEP,
                    "",
                    YMStringConstant.YM_FRAGMENT_TAG_PWD_SET
                )
            }
        })

        closeFcmTv.setOnClickListener(View.OnClickListener {
            if (ConditionUtils.isFastDoubleClick) {
                return@OnClickListener
            }
            YMPasswordDialogFragment.show(
                activity,
                YMParamConstant.YM_VALUE_ACTION_CLOSE_FCM,
                YMStringConstant.YM_FCM_CLOSE_FCM,
                YMStringConstant.YM_FCM_CLOSE_FCM_TIP,
                YMStringConstant.YM_FCM_NEXT_STEP,
                "",
                YMStringConstant.YM_FRAGMENT_TAG_PWD_FCM_CLOSE
            )
        })
    }

    private fun finishEx() {
        dismiss()
        if (isNewFcm) {
            YMManager.instance.setShowFcmPop()
            isNewFcm = false
        }
    }

    override fun isInterceptBackEvent(event: KeyEvent): Boolean {
        return isNightMode
    }

}